package com.styytech.dadabao.api.requestresult;

import android.content.Intent;

import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.api.response.AbstractResponseResult;
import com.styytech.dadabao.ui.login.LoginActivity;
import com.styytech.dadabao.utils.SpUser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Zeus请求结果Model
 *
 * @description
 */
public class RequestResult {

    public final static int SUCCESS = 200;
    public final static int FAILURE = -1;

    /**
     * 构造函数
     *
     * @param httpResponseResult
     */
    public RequestResult(AbstractResponseResult httpResponseResult) {
        this.httpResponseResult = httpResponseResult;
    }

    /**
     * 是否成功
     */
    private boolean success;
    /**
     * 状态码
     */
    private int status;
    /**
     * 额外信息
     */
    private String message;
    /**
     * 业务内容
     */
    private Object responseData;
    /**
     * 完成请求URL
     */
    private String url;

    /**
     * 当前页
     */
    private int page;
    /**
     * 每页数据
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int pages;
    /**
     * 总数据
     */
    private int total;
    /**
     * 详细数据
     */
    private Object rows;


    /**
     * 结果回调
     */
    AbstractResponseResult httpResponseResult;

    public AbstractResponseResult getHttpResponseResult() {
        return httpResponseResult;
    }

    public void setHttpResponseResult(AbstractResponseResult httpResponseResult) {
        this.httpResponseResult = httpResponseResult;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String msg) {
        this.message = msg;
    }

    public Object getResponseData() {
        return responseData;
    }

    public void setResponseData(Object responseData) {
        this.responseData = responseData;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public Object getRows() {
        return rows;
    }

    public void setRows(Object rows) {
        this.rows = rows;
    }

    /**
     * 业务处理，需要根据业务需要自行重写,在重写的是时候，若需使用handler，注意判空
     *
     * @version
     */
    public void doBusiness() {
        if (!isSuccess()) {
            parseError(getRows());
            doError(getStatus());
        }
    }

    private void parseError(Object responseRows) {

        if (responseRows != null) {
            JSONArray jsonArray = (JSONArray) responseRows;
            JSONObject jsonObject = null;
            if (jsonArray != null && !jsonArray.isNull(0)) {
                try {
                    jsonObject = jsonArray.getJSONObject(0);
                    if (jsonObject != null) {
                        setStatus(jsonObject.has("resultCode") ? jsonObject.getInt("resultCode") : -1);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void doError(int status) {
        switch (status) {
            case 200://没有权限
                if (httpResponseResult != null) {
                    httpResponseResult.executeError(getMessage(),
                            AbstractResponseResult.SERVER_FAILURE_LOGIN_FAILURE);
                }
                Intent intent = new Intent(DadaApplication.getContext(), LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                DadaApplication.getContext().startActivity(intent);
                SpUser.clearShare(DadaApplication.getContext());
                break;
            default:
                if (httpResponseResult != null) {
                    httpResponseResult.executeError(getMessage(),
                            AbstractResponseResult.RESPONSE_DATA_FORMAT_ERROR);
                }
                break;
        }
    }
}
